#include <stdio.h>

int main() {
    FILE *file;
    char filename[100];
    char ch;
    int lines = 0, words = 0, chars = 0;
    int in_word = 0;

    printf("Enter the filename to count: ");
    scanf("%s", filename);

    file = fopen(filename, "r");
    
    if (file == NULL) {
        printf("Error: Cannot open file %s\n", filename);
        printf("Please check if the filename is correct and the file exists.\n");
        return 1;
    }

    printf("Counting file %s ...\n", filename);

    while ((ch = fgetc(file)) != EOF) {
        chars++;

        if (ch == '\n') {
            lines++;
        }

        if (ch == ' ' || ch == '\n' || ch == '\t') {
            if (in_word == 1) {
                words++;
                in_word = 0;
            }
        } 
        else {
            in_word = 1;
        }
    }

    if (in_word == 1) {
        words++;
    }

    fclose(file);

    printf("\n========== Counting Results ==========\n");
    printf("File: %s\n", filename);
    printf("Lines: %d\n", lines);
    printf("Words: %d\n", words);
    printf("Characters: %d\n", chars);
    printf("======================================\n");

    return 0;
}